<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="车牌号" prop="vehicleNumber">
        <el-input
          v-model="queryParams.vehicleNumber"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制造商" prop="manufacturer">
        <el-input
          v-model="queryParams.manufacturer"
          placeholder="请输入制造商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车型" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入车型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="载重容量" prop="capacity">
        <el-input
          v-model="queryParams.capacity"
          placeholder="请输入载重容量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择车辆状态" clearable>
          <el-option
            v-for="dict in dict.type.basic_vehicle_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="注册日期" prop="registrationDate">
        <el-date-picker clearable
                        style="width: 205px"
                        v-model="queryParams.registrationDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择注册日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后维护日期" prop="lastMaintenanceDate">
        <el-date-picker clearable
                        style="width: 205px"
                        v-model="queryParams.lastMaintenanceDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择最后维护日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:vehicles:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:vehicles:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:vehicles:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:vehicles:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehiclesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="车牌号" align="center" prop="vehicleNumber" />
      <el-table-column label="制造商" align="center" prop="manufacturer" />
      <el-table-column label="车型" align="center" prop="model" />
      <el-table-column label="载重容量" align="center" prop="capacity" />
      <el-table-column label="车辆状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.basic_vehicle_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="注册日期" align="center" prop="registrationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registrationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后维护日期" align="center" prop="lastMaintenanceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastMaintenanceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="驾驶人姓名" align="center" prop="driverName" />
      <el-table-column label="驾驶人电话" align="center" prop="driverPhoneNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:vehicles:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:vehicles:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改汽车管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="驾驶人" prop="driverId">
          <el-select v-model="form.driverId" filterable placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in driverOptions"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId">
              <span style="float: left">{{ item.userName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.nickName}}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车牌号" prop="vehicleNumber">
          <el-input v-model="form.vehicleNumber" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="制造商" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入制造商" />
        </el-form-item>
        <el-form-item label="车型" prop="model">
          <el-input v-model="form.model" placeholder="请输入车型" />
        </el-form-item>
        <el-form-item label="载重容量" prop="capacity">
          <el-input-number v-model="form.capacity" :min="0" :precision="2"></el-input-number>
        </el-form-item>
        <el-form-item label="车辆状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in dict.type.basic_vehicle_state"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="注册日期" prop="registrationDate" required>
          <el-date-picker clearable
                          v-model="form.registrationDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择注册日期"
                          style="width: 100%">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后维护日期" prop="lastMaintenanceDate" required>
          <el-date-picker clearable
                          v-model="form.lastMaintenanceDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择最后维护日期"
                          style="width: 100%">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVehicles, getVehicles, delVehicles, addVehicles, updateVehicles } from "@/api/basic/vehicles";
import {getWarehouses} from "@/api/basic/warehouses";
import {DRIVER_POST_ID, MANAGER_POST_ID} from "@/api/common";

export default {
  name: "Vehicles",
  dicts: ['basic_vehicle_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 汽车管理表格数据
      vehiclesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      driverOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vehicleNumber: null,
        manufacturer: null,
        model: null,
        capacity: null,
        state: null,
        registrationDate: null,
        lastMaintenanceDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        registrationDate:[
          { required: true, message: "注册日期不能为空", trigger: "blur" }
        ],
        lastMaintenanceDate:[
          { required: true, message: "最后维护日期不能为空", trigger: "blur" }
        ],
        driverId:[
          { required: true, message: "驾驶员不能为空", trigger: "change" }
        ],
        vehicleNumber: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        manufacturer: [
          { required: true, message: "制造商不能为空", trigger: "blur" }
        ],
        model: [
          { required: true, message: "车型不能为空", trigger: "blur" }
        ],
        capacity: [
          { required: true, message: "载重容量不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "车辆状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatDecimal(row, column, cellValue) {
      // 假设您想要保留两位小数
      return Number(cellValue).toFixed(2);
    },
    /** 查询汽车管理列表 */
    getList() {
      this.loading = true;
      listVehicles(this.queryParams).then(response => {
        this.vehiclesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        vehicleId: null,
        vehicleNumber: null,
        manufacturer: null,
        model: null,
        capacity: null,
        state: null,
        registrationDate: null,
        lastMaintenanceDate: null,
        driverId: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.vehicleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getVehicles({
        'postId': DRIVER_POST_ID,
        'vehicleId': null
      }).then(response => {
        this.form = response.data;
        this.driverOptions = response.data.drivers
        this.open = true;
        this.title = "添加汽车管理";
      });

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      console.log(11)
      const vehicleId = row.vehicleId || this.ids
      getVehicles({
        'postId': DRIVER_POST_ID,
        'vehicleId': vehicleId
      }).then(response => {
        this.form = response.data;
        this.driverOptions = response.data.drivers
        this.open = true;
        this.title = "修改汽车管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.vehicleId != null) {
            updateVehicles(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVehicles(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vehicleIds = row.vehicleId || this.ids;
      this.$modal.confirm('是否确认删除汽车管理编号为"' + vehicleIds + '"的数据项？').then(function() {
        return delVehicles(vehicleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basic/vehicles/export', {
        ...this.queryParams
      }, `vehicles_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
